﻿using System;
using System.Collections.Generic;

namespace UniRx.Operators
{
	// Token: 0x0200033B RID: 827
	internal class WhenAllObservable : OperatorObservableBase<Unit>
	{
		// Token: 0x0600111B RID: 4379 RVA: 0x0004ED14 File Offset: 0x0004D114
		public WhenAllObservable(IObservable<Unit>[] sources) : base(false)
		{
			this.sources = sources;
		}

		// Token: 0x0600111C RID: 4380 RVA: 0x0004ED24 File Offset: 0x0004D124
		public WhenAllObservable(IEnumerable<IObservable<Unit>> sources) : base(false)
		{
			this.sourcesEnumerable = sources;
		}

		// Token: 0x0600111D RID: 4381 RVA: 0x0004ED34 File Offset: 0x0004D134
		protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
		{
			if (this.sources != null)
			{
				return new WhenAllObservable.WhenAll(this.sources, observer, cancel).Run();
			}
			IList<IObservable<Unit>> list = this.sourcesEnumerable as IList<IObservable<Unit>>;
			if (list == null)
			{
				list = new List<IObservable<Unit>>(this.sourcesEnumerable);
			}
			return new WhenAllObservable.WhenAll_(list, observer, cancel).Run();
		}

		// Token: 0x04000A1A RID: 2586
		private readonly IObservable<Unit>[] sources;

		// Token: 0x04000A1B RID: 2587
		private readonly IEnumerable<IObservable<Unit>> sourcesEnumerable;

		// Token: 0x0200033C RID: 828
		private class WhenAll : OperatorObserverBase<Unit, Unit>
		{
			// Token: 0x0600111E RID: 4382 RVA: 0x0004ED8A File Offset: 0x0004D18A
			public WhenAll(IObservable<Unit>[] sources, IObserver<Unit> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.sources = sources;
			}

			// Token: 0x0600111F RID: 4383 RVA: 0x0004EDA8 File Offset: 0x0004D1A8
			public IDisposable Run()
			{
				this.length = this.sources.Length;
				if (this.length == 0)
				{
					this.OnNext(Unit.Default);
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
					return Disposable.Empty;
				}
				this.completedCount = 0;
				IDisposable[] array = new IDisposable[this.length];
				for (int i = 0; i < this.sources.Length; i++)
				{
					IObservable<Unit> observable = this.sources[i];
					WhenAllObservable.WhenAll.WhenAllCollectionObserver observer = new WhenAllObservable.WhenAll.WhenAllCollectionObserver(this);
					array[i] = observable.Subscribe(observer);
				}
				return StableCompositeDisposable.CreateUnsafe(array);
			}

			// Token: 0x06001120 RID: 4384 RVA: 0x0004EE54 File Offset: 0x0004D254
			public override void OnNext(Unit value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x06001121 RID: 4385 RVA: 0x0004EE64 File Offset: 0x0004D264
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x06001122 RID: 4386 RVA: 0x0004EE9C File Offset: 0x0004D29C
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A1C RID: 2588
			private readonly IObservable<Unit>[] sources;

			// Token: 0x04000A1D RID: 2589
			private readonly object gate = new object();

			// Token: 0x04000A1E RID: 2590
			private int completedCount;

			// Token: 0x04000A1F RID: 2591
			private int length;

			// Token: 0x0200033D RID: 829
			private class WhenAllCollectionObserver : IObserver<Unit>
			{
				// Token: 0x06001123 RID: 4387 RVA: 0x0004EED4 File Offset: 0x0004D2D4
				public WhenAllCollectionObserver(WhenAllObservable.WhenAll parent)
				{
					this.parent = parent;
				}

				// Token: 0x06001124 RID: 4388 RVA: 0x0004EEE3 File Offset: 0x0004D2E3
				public void OnNext(Unit value)
				{
				}

				// Token: 0x06001125 RID: 4389 RVA: 0x0004EEE8 File Offset: 0x0004D2E8
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.parent.OnError(error);
						}
					}
				}

				// Token: 0x06001126 RID: 4390 RVA: 0x0004EF3C File Offset: 0x0004D33C
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.isCompleted = true;
							this.parent.completedCount++;
							if (this.parent.completedCount == this.parent.length)
							{
								this.parent.OnNext(Unit.Default);
								this.parent.OnCompleted();
							}
						}
					}
				}

				// Token: 0x04000A20 RID: 2592
				private readonly WhenAllObservable.WhenAll parent;

				// Token: 0x04000A21 RID: 2593
				private bool isCompleted;
			}
		}

		// Token: 0x0200033E RID: 830
		private class WhenAll_ : OperatorObserverBase<Unit, Unit>
		{
			// Token: 0x06001127 RID: 4391 RVA: 0x0004EFD4 File Offset: 0x0004D3D4
			public WhenAll_(IList<IObservable<Unit>> sources, IObserver<Unit> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.sources = sources;
			}

			// Token: 0x06001128 RID: 4392 RVA: 0x0004EFF0 File Offset: 0x0004D3F0
			public IDisposable Run()
			{
				this.length = this.sources.Count;
				if (this.length == 0)
				{
					this.OnNext(Unit.Default);
					try
					{
						this.observer.OnCompleted();
					}
					finally
					{
						base.Dispose();
					}
					return Disposable.Empty;
				}
				this.completedCount = 0;
				IDisposable[] array = new IDisposable[this.length];
				for (int i = 0; i < this.length; i++)
				{
					IObservable<Unit> observable = this.sources[i];
					WhenAllObservable.WhenAll_.WhenAllCollectionObserver observer = new WhenAllObservable.WhenAll_.WhenAllCollectionObserver(this);
					array[i] = observable.Subscribe(observer);
				}
				return StableCompositeDisposable.CreateUnsafe(array);
			}

			// Token: 0x06001129 RID: 4393 RVA: 0x0004F0A4 File Offset: 0x0004D4A4
			public override void OnNext(Unit value)
			{
				this.observer.OnNext(value);
			}

			// Token: 0x0600112A RID: 4394 RVA: 0x0004F0B4 File Offset: 0x0004D4B4
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600112B RID: 4395 RVA: 0x0004F0EC File Offset: 0x0004D4EC
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x04000A22 RID: 2594
			private readonly IList<IObservable<Unit>> sources;

			// Token: 0x04000A23 RID: 2595
			private readonly object gate = new object();

			// Token: 0x04000A24 RID: 2596
			private int completedCount;

			// Token: 0x04000A25 RID: 2597
			private int length;

			// Token: 0x0200033F RID: 831
			private class WhenAllCollectionObserver : IObserver<Unit>
			{
				// Token: 0x0600112C RID: 4396 RVA: 0x0004F124 File Offset: 0x0004D524
				public WhenAllCollectionObserver(WhenAllObservable.WhenAll_ parent)
				{
					this.parent = parent;
				}

				// Token: 0x0600112D RID: 4397 RVA: 0x0004F133 File Offset: 0x0004D533
				public void OnNext(Unit value)
				{
				}

				// Token: 0x0600112E RID: 4398 RVA: 0x0004F138 File Offset: 0x0004D538
				public void OnError(Exception error)
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.parent.OnError(error);
						}
					}
				}

				// Token: 0x0600112F RID: 4399 RVA: 0x0004F18C File Offset: 0x0004D58C
				public void OnCompleted()
				{
					object gate = this.parent.gate;
					lock (gate)
					{
						if (!this.isCompleted)
						{
							this.isCompleted = true;
							this.parent.completedCount++;
							if (this.parent.completedCount == this.parent.length)
							{
								this.parent.OnNext(Unit.Default);
								this.parent.OnCompleted();
							}
						}
					}
				}

				// Token: 0x04000A26 RID: 2598
				private readonly WhenAllObservable.WhenAll_ parent;

				// Token: 0x04000A27 RID: 2599
				private bool isCompleted;
			}
		}
	}
}
